<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Admin\UserService;
use App\Http\Requests\Admin\UserRequest;

class UserController extends Controller
{

    public function options() {
        $data = UserService::options();

        return $this->success(data: $data);
    }

    public function passwordReset(Request $request, string $id)
    {

        $password = $request->validate([
            'password' => 'required|string'
        ])['password'];

        $data = UserService::passwordReset($password, $id);

        return $this->success(data: $data);
    }

    public function me()
    {

        $data = UserService::me();

        return $this->success(data: $data);
    }

    /**
     * 显示资源列表。
     */
    public function index(Request $request)
    {
        $params = $request->validate([
            'keywords'     => 'nullable|string',
            'status'       => 'nullable|integer|in:0,1',
            'deptId'       => 'nullable|integer',
            'createTime'   => 'nullable|array|size:2',
            'createTime.*' => 'date',
            'pageNum'      => 'nullable|integer|min:1',
            'pageSize'     => 'nullable|integer|min:5',
        ]);

        $params['pageNum'] = $params['pageNum'] ?? 1;
        $params['pageSize'] = $params['pageSize'] ?? 10;

        $data = UserService::index($params);

        return $this->success(data: $data);
    }

    /**
     * 将新创建的资源存储到数据库。
     */
    public function store(UserRequest $request)
    {
        $validated = $request->validated();

        $data = UserService::store($validated);

        return $this->success(data: $data);
    }

    /**
     * 显示指定资源的详细信息。
     */
    public function show(string $id)
    {
        $data = UserService::show($id);

        return $this->success(data: $data);
    }

    /**
     * 更新指定资源。
     */
    public function update(UserRequest $request, string $id)
    {
        $validated = $request->validated();

        $data = UserService::update($validated, $id);

        return $this->success(data: $data);
    }

    /**
     * 删除指定资源。
     */
    public function destroy(string $id)
    {
        $data = UserService::destroy($id);

        return $this->success(data: $data);
    }
}
